package com.neuedu.ud;

// import com.neuedu.updown.entity.UploadFile;
// import com.neuedu.updown.service.UploadFileService;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

/**
 * 项目：      javawebother
 * 类名:       ${NAME}
 * 创建时间：  2020/12/18  9:29
 * 描述 :     ${dc}
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebServlet(name = "FileUploadServlet", urlPatterns = "/up")
@MultipartConfig(location="D:\\upload")
public class FileUploadServlet extends HttpServlet {

    public static final String baseDir ="D:\\upload";

    // private UploadFileService uploadFileService = new UploadFileService();

    // private static String baseDir;
    // static {
    //
    //     InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
    //     Properties properties = new Properties();
    //     try {
    //         properties.load(is);
    //
    //         baseDir = (String) properties.getProperty("upload.dir");
    //     } catch (IOException e) {
    //         e.printStackTrace();
    //     }
    // }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        Part part = request.getPart("file");

//        request.getParts()
        String contentDisposition = part.getHeader("content-disposition"); // form-data; name="file"; filename="xxxx"

        //原始的文件名
        String fileName = contentDisposition.split("filename")[1] ;//  ="xxxx"

        fileName = fileName.substring(2,fileName.length()-1);

        System.out.println("fileName:"+fileName);


        String ext = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replace("-", "") + ext;
        // File dist = new File(baseDir, newFileName); //使用uuid 生成不重复的 文件名
        // dist.createNewFile();
        // String writeFileName = dist.getPath();
        part.write(newFileName);//转储到 baseDir【D:/upload】

        String now = "";//new Date();
        String clientHost = request.getRemoteHost();
        long size = part.getSize();

        String info = String.format("host:%s\t time:%s, name:%s\t size:%s",
                clientHost,
                new Date().toString(),
                fileName,
                String.valueOf(size)
        );

        //上传文件的信息
        System.out.println(info);


        // UploadFile uploadFile = new UploadFile();
        // uploadFile.setIp(clientHost);
        // uploadFile.setOriginName(fileName);
        // uploadFile.setPath(newFileName);
        // uploadFile.setSize((int) size);
        //
        // uploadFileService.save(uploadFile);


        //查询
        // response.sendRedirect(request.getContextPath()+"/filelist");


    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}